<!DOCTYPE html>
<html lang=zh>
<head>
    <!-- hexo-inject:begin --><!-- hexo-inject:end --><!-- so meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="HandheldFriendly" content="True">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=5" />
    <meta name="description" content="前言学习Python良久，想全面的回顾一下Python的基础知识，于是阅读了Python3.4的官方教程；在这里做要点的摘录，只为提纲挈领，对细节不做过多的解释。 Control Flow Tools坑 Data Structureslist, dict, set, tuple, str More on Lists methods:append(x), extend(L), insert(i, x">
<meta property="og:type" content="article">
<meta property="og:title" content="Python tutorial 摘录">
<meta property="og:url" content="https://hillbamboo.gitee.io/2017/05/22/pydoc01/index.html">
<meta property="og:site_name" content="拿铁轮的自留地">
<meta property="og:description" content="前言学习Python良久，想全面的回顾一下Python的基础知识，于是阅读了Python3.4的官方教程；在这里做要点的摘录，只为提纲挈领，对细节不做过多的解释。 Control Flow Tools坑 Data Structureslist, dict, set, tuple, str More on Lists methods:append(x), extend(L), insert(i, x">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2017-05-22T09:12:05.000Z">
<meta property="article:modified_time" content="2021-05-31T23:57:54.884Z">
<meta property="article:author" content="拿铁轮">
<meta name="twitter:card" content="summary">
    
    
      
        
          <link rel="shortcut icon" href="/images/favicon.ico">
        
      
      
        
          <link rel="icon" type="image/png" href="/images/favicon-192x192.png" sizes="192x192">
        
      
      
        
          <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon.png">
        
      
    
    <!-- title -->
    <title>Python tutorial 摘录</title>
    <!-- styles -->
    
<link rel="stylesheet" href="/css/style.css">

    <!-- persian styles -->
    
    <!-- rss -->
    
    
      <link rel="alternate" href="/true" title="拿铁轮的自留地" type="application/atom+xml" />
    
	<!-- mathjax -->
	
<meta name="generator" content="Hexo 4.2.1"><!-- hexo-inject:begin --><!-- hexo-inject:end --></head>

<body class="max-width mx-auto px3 ltr">
    
      <!-- hexo-inject:begin --><!-- hexo-inject:end --><div id="header-post">
  <a id="menu-icon" href="#" aria-label="目录"><i class="fas fa-bars fa-lg"></i></a>
  <a id="menu-icon-tablet" href="#" aria-label="目录"><i class="fas fa-bars fa-lg"></i></a>
  <a id="top-icon-tablet" href="#" aria-label="顶部" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');" style="display:none;"><i class="fas fa-chevron-up fa-lg"></i></a>
  <span id="menu">
    <span id="nav">
      <ul>
        <!--
       --><li><a href="/">首页</a></li><!--
     --><!--
       --><li><a href="/about/">关于</a></li><!--
     --><!--
       --><li><a href="/archives/">归档</a></li><!--
     --><!--
       --><li><a href="/categories/">分类</a></li><!--
     --><!--
       --><li><a href="/tags/">标签</a></li><!--
     --><!--
       --><li><a href="/search/">搜索</a></li><!--
     -->
      </ul>
    </span>
    <br/>
    <span id="actions">
      <ul>
        
        <li><a class="icon" aria-label="上一篇" href="/2018/03/04/zzh-ML/"><i class="fas fa-chevron-left" aria-hidden="true" onmouseover="$('#i-prev').toggle();" onmouseout="$('#i-prev').toggle();"></i></a></li>
        
        
        <li><a class="icon" aria-label="下一篇" href="/2017/05/16/a-byte-of-pyhton-note-01/"><i class="fas fa-chevron-right" aria-hidden="true" onmouseover="$('#i-next').toggle();" onmouseout="$('#i-next').toggle();"></i></a></li>
        
        <li><a class="icon" aria-label="返回顶部" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up" aria-hidden="true" onmouseover="$('#i-top').toggle();" onmouseout="$('#i-top').toggle();"></i></a></li>
        <li><a class="icon" aria-label="分享文章" href="#"><i class="fas fa-share-alt" aria-hidden="true" onmouseover="$('#i-share').toggle();" onmouseout="$('#i-share').toggle();" onclick="$('#share').toggle();return false;"></i></a></li>
      </ul>
      <span id="i-prev" class="info" style="display:none;">上一篇</span>
      <span id="i-next" class="info" style="display:none;">下一篇</span>
      <span id="i-top" class="info" style="display:none;">返回顶部</span>
      <span id="i-share" class="info" style="display:none;">分享文章</span>
    </span>
    <br/>
    <div id="share" style="display: none">
      <ul>
  <li><a class="icon" href="http://www.facebook.com/sharer.php?u=https://hillbamboo.gitee.io/2017/05/22/pydoc01/" target="_blank" rel="noopener"><i class="fab fa-facebook " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://twitter.com/share?url=https://hillbamboo.gitee.io/2017/05/22/pydoc01/&text=Python tutorial 摘录" target="_blank" rel="noopener"><i class="fab fa-twitter " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.linkedin.com/shareArticle?url=https://hillbamboo.gitee.io/2017/05/22/pydoc01/&title=Python tutorial 摘录" target="_blank" rel="noopener"><i class="fab fa-linkedin " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://service.weibo.com/share/share.php?url=https://hillbamboo.gitee.io/2017/05/22/pydoc01/&title=Python tutorial 摘录" target="_blank" rel="noopener"><i class="fab fa-weibo " aria-hidden="true"></i></a></li>
  <!-- <li><a class="icon" href="https://pinterest.com/pin/create/bookmarklet/?url=https://hillbamboo.gitee.io/2017/05/22/pydoc01/&is_video=false&description=Python tutorial 摘录" target="_blank" rel="noopener"><i class="fab fa-pinterest " aria-hidden="true"></i></a></li> -->
  <li><a class="icon" href="mailto:?subject=Python tutorial 摘录&body=Check out this article: https://hillbamboo.gitee.io/2017/05/22/pydoc01/"><i class="fas fa-envelope " aria-hidden="true"></i></a></li>
  <!-- <li><a class="icon" href="https://getpocket.com/save?url=https://hillbamboo.gitee.io/2017/05/22/pydoc01/&title=Python tutorial 摘录" target="_blank" rel="noopener"><i class="fab fa-get-pocket " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://reddit.com/submit?url=https://hillbamboo.gitee.io/2017/05/22/pydoc01/&title=Python tutorial 摘录" target="_blank" rel="noopener"><i class="fab fa-reddit " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.stumbleupon.com/submit?url=https://hillbamboo.gitee.io/2017/05/22/pydoc01/&title=Python tutorial 摘录" target="_blank" rel="noopener"><i class="fab fa-stumbleupon " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://digg.com/submit?url=https://hillbamboo.gitee.io/2017/05/22/pydoc01/&title=Python tutorial 摘录" target="_blank" rel="noopener"><i class="fab fa-digg " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.tumblr.com/share/link?url=https://hillbamboo.gitee.io/2017/05/22/pydoc01/&name=Python tutorial 摘录&description=" target="_blank" rel="noopener"><i class="fab fa-tumblr " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://news.ycombinator.com/submitlink?u=https://hillbamboo.gitee.io/2017/05/22/pydoc01/&t=Python tutorial 摘录" target="_blank" rel="noopener"><i class="fab fa-hacker-news " aria-hidden="true"></i></a></li> -->
</ul>



    </div>
    <div id="toc">
      <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#前言"><span class="toc-number">1.</span> <span class="toc-text">前言</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Control-Flow-Tools"><span class="toc-number">2.</span> <span class="toc-text">Control Flow Tools</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Data-Structures"><span class="toc-number">3.</span> <span class="toc-text">Data Structures</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#More-on-Lists"><span class="toc-number">3.1.</span> <span class="toc-text">More on Lists</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Comprehensions"><span class="toc-number">3.2.</span> <span class="toc-text">Comprehensions</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Looping-Techniques"><span class="toc-number">3.3.</span> <span class="toc-text">Looping Techniques</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Comparing-Sequences-and-Other-Types"><span class="toc-number">3.4.</span> <span class="toc-text">Comparing Sequences and Other Types</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Modules-and-Packages"><span class="toc-number">4.</span> <span class="toc-text">Modules and Packages</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Input-and-Output"><span class="toc-number">5.</span> <span class="toc-text">Input and Output</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Fancier-Output-Formatting"><span class="toc-number">5.1.</span> <span class="toc-text">Fancier Output Formatting</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Class"><span class="toc-number">6.</span> <span class="toc-text">Class</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#A-word-About-Names-and-Objects"><span class="toc-number">6.1.</span> <span class="toc-text">A word About Names and Objects</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Python-Scopes-and-Namespaces"><span class="toc-number">6.2.</span> <span class="toc-text">Python Scopes and Namespaces</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Odds-amp-Ends"><span class="toc-number">6.3.</span> <span class="toc-text">Odds &amp; Ends</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Iterator-amp-Generator"><span class="toc-number">6.4.</span> <span class="toc-text">Iterator &amp; Generator</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#Iterator"><span class="toc-number">6.4.1.</span> <span class="toc-text">Iterator</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Generator"><span class="toc-number">6.4.2.</span> <span class="toc-text">Generator</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Generator-Expressions"><span class="toc-number">6.4.3.</span> <span class="toc-text">Generator Expressions</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Brief-Tour-of-Standard-Library"><span class="toc-number">7.</span> <span class="toc-text">Brief Tour of Standard Library</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Reference"><span class="toc-number">8.</span> <span class="toc-text">Reference</span></a></li></ol>
    </div>
  </span>
</div>

    
    <div class="content index py4">
        
        <article class="post" itemscope itemtype="http://schema.org/BlogPosting">
  <header>
    
    <h1 class="posttitle" itemprop="name headline">
        Python tutorial 摘录
    </h1>



    <div class="meta">
      <span class="author" itemprop="author" itemscope itemtype="http://schema.org/Person">
        <span itemprop="name">拿铁轮</span>
      </span>
      
    <div class="postdate">
      
        <time datetime="2017-05-22T09:12:05.000Z" itemprop="datePublished">2017-05-22</time>
        
      
    </div>


      

      

    </div>
  </header>
  

  <div class="content" itemprop="articleBody">
    <h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>学习Python良久，想全面的回顾一下Python的基础知识，于是阅读了Python3.4的官方教程；在这里做要点的摘录，只为提纲挈领，对细节不做过多的解释。</p>
<h2 id="Control-Flow-Tools"><a href="#Control-Flow-Tools" class="headerlink" title="Control Flow Tools"></a>Control Flow Tools</h2><p>坑</p>
<h2 id="Data-Structures"><a href="#Data-Structures" class="headerlink" title="Data Structures"></a>Data Structures</h2><p>list, dict, set, tuple, str</p>
<h3 id="More-on-Lists"><a href="#More-on-Lists" class="headerlink" title="More on Lists"></a>More on Lists</h3><ol>
<li>methods:<br>append(x), extend(L), insert(i, x), remove(x), pop([i]), clear(), index(x), count(x), sort(), reverse(), copy()</li>
<li>Using Lists as Stacks(OK)</li>
<li>Using Lists as Queues(Not efficient) -&gt; Using collections.deque</li>
</ol>
<h3 id="Comprehensions"><a href="#Comprehensions" class="headerlink" title="Comprehensions"></a>Comprehensions</h3><p>List/Set/Dictionary Comprehensions</p>
<h3 id="Looping-Techniques"><a href="#Looping-Techniques" class="headerlink" title="Looping Techniques"></a>Looping Techniques</h3><ol>
<li>Using the <code>items()</code> method to loop over dictionaries</li>
<li>Using the <code>enumerate()</code> function to loop over a sequence</li>
<li>Using the <code>zip()</code> function to loop over two or more sequences at the same time</li>
<li>Using the <code>reversed()</code> function to loop over a sequence in reverse</li>
<li>Using the  <code>sorted()</code> function to loop over a sequence in sorted order</li>
</ol>
<h3 id="Comparing-Sequences-and-Other-Types"><a href="#Comparing-Sequences-and-Other-Types" class="headerlink" title="Comparing Sequences and Other Types"></a>Comparing Sequences and Other Types</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>)              &lt; (<span class="number">1</span>, <span class="number">2</span>, <span class="number">4</span>)</span><br><span class="line">[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]              &lt; [<span class="number">1</span>, <span class="number">2</span>, <span class="number">4</span>]</span><br><span class="line"><span class="string">'ABC'</span> &lt; <span class="string">'C'</span> &lt; <span class="string">'Pascal'</span> &lt; <span class="string">'Python'</span> <span class="comment"># Comparisons can be chained</span></span><br><span class="line">(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>)           &lt; (<span class="number">1</span>, <span class="number">2</span>, <span class="number">4</span>)</span><br><span class="line">(<span class="number">1</span>, <span class="number">2</span>)                 &lt; (<span class="number">1</span>, <span class="number">2</span>, <span class="number">-1</span>)</span><br><span class="line">(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>)             == (<span class="number">1.0</span>, <span class="number">2.0</span>, <span class="number">3.0</span>)</span><br><span class="line">(<span class="number">1</span>, <span class="number">2</span>, (<span class="string">'aa'</span>, <span class="string">'ab'</span>))   &lt; (<span class="number">1</span>, <span class="number">2</span>, (<span class="string">'abc'</span>, <span class="string">'a'</span>), <span class="number">4</span>)</span><br></pre></td></tr></table></figure>
<h2 id="Modules-and-Packages"><a href="#Modules-and-Packages" class="headerlink" title="Modules and Packages"></a>Modules and Packages</h2><h2 id="Input-and-Output"><a href="#Input-and-Output" class="headerlink" title="Input and Output"></a>Input and Output</h2><h3 id="Fancier-Output-Formatting"><a href="#Fancier-Output-Formatting" class="headerlink" title="Fancier Output Formatting"></a>Fancier Output Formatting</h3><ul>
<li>The str() function is meant to return representations of values which are fairly human-readable, while repr() is meant to generate representations which can be read by the interpreter</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">for</span> x <span class="keyword">in</span> range(<span class="number">1</span>, <span class="number">11</span>):</span><br><span class="line"><span class="meta">... </span>    print(repr(x).rjust(<span class="number">2</span>), repr(x*x).rjust(<span class="number">3</span>), end=<span class="string">' '</span>)</span><br><span class="line"><span class="meta">... </span>    <span class="comment"># Note use of 'end' on previous line</span></span><br><span class="line"><span class="meta">... </span>    print(repr(x*x*x).rjust(<span class="number">4</span>))</span><br><span class="line">...</span><br><span class="line"> <span class="number">1</span>   <span class="number">1</span>    <span class="number">1</span></span><br><span class="line"> <span class="number">2</span>   <span class="number">4</span>    <span class="number">8</span></span><br><span class="line"> <span class="number">3</span>   <span class="number">9</span>   <span class="number">27</span></span><br><span class="line"> <span class="number">4</span>  <span class="number">16</span>   <span class="number">64</span></span><br><span class="line"> <span class="number">5</span>  <span class="number">25</span>  <span class="number">125</span></span><br><span class="line"> <span class="number">6</span>  <span class="number">36</span>  <span class="number">216</span></span><br><span class="line"> <span class="number">7</span>  <span class="number">49</span>  <span class="number">343</span></span><br><span class="line"> <span class="number">8</span>  <span class="number">64</span>  <span class="number">512</span></span><br><span class="line"> <span class="number">9</span>  <span class="number">81</span>  <span class="number">729</span></span><br><span class="line"><span class="number">10</span> <span class="number">100</span> <span class="number">1000</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">for</span> x <span class="keyword">in</span> range(<span class="number">1</span>, <span class="number">11</span>):</span><br><span class="line"><span class="meta">... </span>    print(<span class="string">'&#123;0:2d&#125; &#123;1:3d&#125; &#123;2:4d&#125;'</span>.format(x, x*x, x*x*x))</span><br><span class="line">...</span><br><span class="line"> <span class="number">1</span>   <span class="number">1</span>    <span class="number">1</span></span><br><span class="line"> <span class="number">2</span>   <span class="number">4</span>    <span class="number">8</span></span><br><span class="line"> <span class="number">3</span>   <span class="number">9</span>   <span class="number">27</span></span><br><span class="line"> <span class="number">4</span>  <span class="number">16</span>   <span class="number">64</span></span><br><span class="line"> <span class="number">5</span>  <span class="number">25</span>  <span class="number">125</span></span><br><span class="line"> <span class="number">6</span>  <span class="number">36</span>  <span class="number">216</span></span><br><span class="line"> <span class="number">7</span>  <span class="number">49</span>  <span class="number">343</span></span><br><span class="line"> <span class="number">8</span>  <span class="number">64</span>  <span class="number">512</span></span><br><span class="line"> <span class="number">9</span>  <span class="number">81</span>  <span class="number">729</span></span><br><span class="line"><span class="number">10</span> <span class="number">100</span> <span class="number">1000</span></span><br></pre></td></tr></table></figure>
<ul>
<li>str.format()<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(<span class="string">'We are the &#123;&#125; who say "&#123;&#125;!"'</span>.format(<span class="string">'knights'</span>, <span class="string">'Ni'</span>))</span><br><span class="line">We are the knights who say <span class="string">"Ni!"</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(<span class="string">'This &#123;food&#125; is &#123;adjective&#125;.'</span>.format(</span><br><span class="line"><span class="meta">... </span>      food=<span class="string">'spam'</span>, adjective=<span class="string">'absolutely horrible'</span>))</span><br><span class="line">This spam <span class="keyword">is</span> absolutely horrible.</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(<span class="string">'The story of &#123;0&#125;, &#123;1&#125;, and &#123;other&#125;.'</span>.format(<span class="string">'Bill'</span>, <span class="string">'Manfred'</span>,other=<span class="string">'Georg'</span>))</span><br><span class="line">The story of Bill, Manfred, <span class="keyword">and</span> Georg.</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>table = &#123;<span class="string">'Sjoerd'</span>: <span class="number">4127</span>, <span class="string">'Jack'</span>: <span class="number">4098</span>, <span class="string">'Dcab'</span>: <span class="number">7678</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">for</span> name, phone <span class="keyword">in</span> table.items():</span><br><span class="line"><span class="meta">... </span>    print(<span class="string">'&#123;0:10&#125; ==&gt; &#123;1:10d&#125;'</span>.format(name, phone))</span><br><span class="line">...</span><br><span class="line">Jack       ==&gt;       4098</span><br><span class="line">Dcab       ==&gt;       7678</span><br><span class="line">Sjoerd     ==&gt;       4127</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>table = &#123;<span class="string">'Sjoerd'</span>: <span class="number">4127</span>, <span class="string">'Jack'</span>: <span class="number">4098</span>, <span class="string">'Dcab'</span>: <span class="number">8637678</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(<span class="string">'Jack: &#123;0[Jack]:d&#125;; Sjoerd: &#123;0[Sjoerd]:d&#125;; '</span></span><br><span class="line"><span class="meta">... </span>      <span class="string">'Dcab: &#123;0[Dcab]:d&#125;'</span>.format(table))</span><br><span class="line">Jack: <span class="number">4098</span>; Sjoerd: <span class="number">4127</span>; Dcab: <span class="number">8637678</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>table = &#123;<span class="string">'Sjoerd'</span>: <span class="number">4127</span>, <span class="string">'Jack'</span>: <span class="number">4098</span>, <span class="string">'Dcab'</span>: <span class="number">8637678</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(<span class="string">'Jack: &#123;Jack:d&#125;; Sjoerd: &#123;Sjoerd:d&#125;; Dcab: &#123;Dcab:d&#125;'</span>.format(**table))</span><br><span class="line">Jack: <span class="number">4098</span>; Sjoerd: <span class="number">4127</span>; Dcab: <span class="number">8637678</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="Class"><a href="#Class" class="headerlink" title="Class"></a>Class</h2><h3 id="A-word-About-Names-and-Objects"><a href="#A-word-About-Names-and-Objects" class="headerlink" title="A word About Names and Objects"></a>A word About Names and Objects</h3><p>Aliasing has a possibly surprising effect on the semantics of Python code involving mutable objects such as lists, dictionaries, and most other types.</p>
<h3 id="Python-Scopes-and-Namespaces"><a href="#Python-Scopes-and-Namespaces" class="headerlink" title="Python Scopes and Namespaces"></a>Python Scopes and Namespaces</h3><h3 id="Odds-amp-Ends"><a href="#Odds-amp-Ends" class="headerlink" title="Odds &amp; Ends"></a>Odds &amp; Ends</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Employee</span>:</span></span><br><span class="line">    <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line">john = Employee() <span class="comment"># Create an empty employee record</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Fill the fields of the record</span></span><br><span class="line">john.name = <span class="string">'John Doe'</span></span><br><span class="line">john.dept = <span class="string">'computer lab'</span></span><br><span class="line">john.salary = <span class="number">1000</span></span><br></pre></td></tr></table></figure>
<h3 id="Iterator-amp-Generator"><a href="#Iterator-amp-Generator" class="headerlink" title="Iterator &amp; Generator"></a>Iterator &amp; Generator</h3><h4 id="Iterator"><a href="#Iterator" class="headerlink" title="Iterator"></a>Iterator</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>s = <span class="string">'abc'</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>it = iter(s)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>it</span><br><span class="line">&lt;iterator object at <span class="number">0x00A1DB50</span>&gt;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>next(it)</span><br><span class="line"><span class="string">'a'</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>next(it)</span><br><span class="line"><span class="string">'b'</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>next(it)</span><br><span class="line"><span class="string">'c'</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>next(it)</span><br><span class="line">Traceback (most recent call last):</span><br><span class="line">  File "&lt;stdin&gt;", line 1, in ?</span><br><span class="line">    next(it)</span><br><span class="line">StopIteration</span><br></pre></td></tr></table></figure>
<h4 id="Generator"><a href="#Generator" class="headerlink" title="Generator"></a>Generator</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="function"><span class="keyword">def</span> <span class="title">reverse</span><span class="params">(data)</span>:</span></span><br><span class="line"><span class="meta">... </span>    <span class="keyword">for</span> index <span class="keyword">in</span> range(len(data)<span class="number">-1</span>, <span class="number">-1</span>, <span class="number">-1</span>):</span><br><span class="line"><span class="meta">... </span>        <span class="keyword">yield</span> data[index]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">for</span> char <span class="keyword">in</span> reverse(<span class="string">'golf'</span>):</span><br><span class="line"><span class="meta">... </span>    print(char)</span><br><span class="line">...</span><br><span class="line">f</span><br><span class="line">l</span><br><span class="line">o</span><br><span class="line">g</span><br></pre></td></tr></table></figure>
<h4 id="Generator-Expressions"><a href="#Generator-Expressions" class="headerlink" title="Generator Expressions"></a>Generator Expressions</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>sum(i*i <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">10</span>))                 <span class="comment"># sum of squares</span></span><br><span class="line"><span class="number">285</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>xvec = [<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>yvec = [<span class="number">7</span>, <span class="number">5</span>, <span class="number">3</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>sum(x*y <span class="keyword">for</span> x,y <span class="keyword">in</span> zip(xvec, yvec))         <span class="comment"># dot product</span></span><br><span class="line"><span class="number">260</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">from</span> math <span class="keyword">import</span> pi, sin</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>sine_table = &#123;x: sin(x*pi/<span class="number">180</span>) <span class="keyword">for</span> x <span class="keyword">in</span> range(<span class="number">0</span>, <span class="number">91</span>)&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>unique_words = set(word  <span class="keyword">for</span> line <span class="keyword">in</span> page  <span class="keyword">for</span> word <span class="keyword">in</span> line.split())</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>valedictorian = max((student.gpa, student.name) <span class="keyword">for</span> student <span class="keyword">in</span> graduates)</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>data = <span class="string">'golf'</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>list(data[i] <span class="keyword">for</span> i <span class="keyword">in</span> range(len(data)<span class="number">-1</span>, <span class="number">-1</span>, <span class="number">-1</span>))</span><br><span class="line">[<span class="string">'f'</span>, <span class="string">'l'</span>, <span class="string">'o'</span>, <span class="string">'g'</span>]</span><br></pre></td></tr></table></figure>
<h2 id="Brief-Tour-of-Standard-Library"><a href="#Brief-Tour-of-Standard-Library" class="headerlink" title="Brief Tour of Standard Library"></a>Brief Tour of Standard Library</h2><p>坑</p>
<h2 id="Reference"><a href="#Reference" class="headerlink" title="Reference"></a>Reference</h2><p><a href="https://docs.python.org/3.4/tutorial/index.html" target="_blank" rel="noopener">The Python Tutorial</a></p>

  </div>
</article>

    <div class="blog-post-comments">
        <div id="disqus_thread">
            <noscript>加载评论需要在浏览器启用 JavaScript 脚本支持。</noscript>
        </div>
    </div>



        
          <div id="footer-post-container">
  <div id="footer-post">

    <div id="nav-footer" style="display: none">
      <ul>
         
          <li><a href="/">首页</a></li>
         
          <li><a href="/about/">关于</a></li>
         
          <li><a href="/archives/">归档</a></li>
         
          <li><a href="/categories/">分类</a></li>
         
          <li><a href="/tags/">标签</a></li>
         
          <li><a href="/search/">搜索</a></li>
        
      </ul>
    </div>

    <div id="toc-footer" style="display: none">
      <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#前言"><span class="toc-number">1.</span> <span class="toc-text">前言</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Control-Flow-Tools"><span class="toc-number">2.</span> <span class="toc-text">Control Flow Tools</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Data-Structures"><span class="toc-number">3.</span> <span class="toc-text">Data Structures</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#More-on-Lists"><span class="toc-number">3.1.</span> <span class="toc-text">More on Lists</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Comprehensions"><span class="toc-number">3.2.</span> <span class="toc-text">Comprehensions</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Looping-Techniques"><span class="toc-number">3.3.</span> <span class="toc-text">Looping Techniques</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Comparing-Sequences-and-Other-Types"><span class="toc-number">3.4.</span> <span class="toc-text">Comparing Sequences and Other Types</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Modules-and-Packages"><span class="toc-number">4.</span> <span class="toc-text">Modules and Packages</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Input-and-Output"><span class="toc-number">5.</span> <span class="toc-text">Input and Output</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Fancier-Output-Formatting"><span class="toc-number">5.1.</span> <span class="toc-text">Fancier Output Formatting</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Class"><span class="toc-number">6.</span> <span class="toc-text">Class</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#A-word-About-Names-and-Objects"><span class="toc-number">6.1.</span> <span class="toc-text">A word About Names and Objects</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Python-Scopes-and-Namespaces"><span class="toc-number">6.2.</span> <span class="toc-text">Python Scopes and Namespaces</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Odds-amp-Ends"><span class="toc-number">6.3.</span> <span class="toc-text">Odds &amp; Ends</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Iterator-amp-Generator"><span class="toc-number">6.4.</span> <span class="toc-text">Iterator &amp; Generator</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#Iterator"><span class="toc-number">6.4.1.</span> <span class="toc-text">Iterator</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Generator"><span class="toc-number">6.4.2.</span> <span class="toc-text">Generator</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Generator-Expressions"><span class="toc-number">6.4.3.</span> <span class="toc-text">Generator Expressions</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Brief-Tour-of-Standard-Library"><span class="toc-number">7.</span> <span class="toc-text">Brief Tour of Standard Library</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Reference"><span class="toc-number">8.</span> <span class="toc-text">Reference</span></a></li></ol>
    </div>

    <div id="share-footer" style="display: none">
      <ul>
  <li><a class="icon" href="http://www.facebook.com/sharer.php?u=https://hillbamboo.gitee.io/2017/05/22/pydoc01/" target="_blank" rel="noopener"><i class="fab fa-facebook fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://twitter.com/share?url=https://hillbamboo.gitee.io/2017/05/22/pydoc01/&text=Python tutorial 摘录" target="_blank" rel="noopener"><i class="fab fa-twitter fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.linkedin.com/shareArticle?url=https://hillbamboo.gitee.io/2017/05/22/pydoc01/&title=Python tutorial 摘录" target="_blank" rel="noopener"><i class="fab fa-linkedin fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://service.weibo.com/share/share.php?url=https://hillbamboo.gitee.io/2017/05/22/pydoc01/&title=Python tutorial 摘录" target="_blank" rel="noopener"><i class="fab fa-weibo fa-lg" aria-hidden="true"></i></a></li>
  <!-- <li><a class="icon" href="https://pinterest.com/pin/create/bookmarklet/?url=https://hillbamboo.gitee.io/2017/05/22/pydoc01/&is_video=false&description=Python tutorial 摘录" target="_blank" rel="noopener"><i class="fab fa-pinterest fa-lg" aria-hidden="true"></i></a></li> -->
  <li><a class="icon" href="mailto:?subject=Python tutorial 摘录&body=Check out this article: https://hillbamboo.gitee.io/2017/05/22/pydoc01/"><i class="fas fa-envelope fa-lg" aria-hidden="true"></i></a></li>
  <!-- <li><a class="icon" href="https://getpocket.com/save?url=https://hillbamboo.gitee.io/2017/05/22/pydoc01/&title=Python tutorial 摘录" target="_blank" rel="noopener"><i class="fab fa-get-pocket fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://reddit.com/submit?url=https://hillbamboo.gitee.io/2017/05/22/pydoc01/&title=Python tutorial 摘录" target="_blank" rel="noopener"><i class="fab fa-reddit fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.stumbleupon.com/submit?url=https://hillbamboo.gitee.io/2017/05/22/pydoc01/&title=Python tutorial 摘录" target="_blank" rel="noopener"><i class="fab fa-stumbleupon fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://digg.com/submit?url=https://hillbamboo.gitee.io/2017/05/22/pydoc01/&title=Python tutorial 摘录" target="_blank" rel="noopener"><i class="fab fa-digg fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.tumblr.com/share/link?url=https://hillbamboo.gitee.io/2017/05/22/pydoc01/&name=Python tutorial 摘录&description=" target="_blank" rel="noopener"><i class="fab fa-tumblr fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://news.ycombinator.com/submitlink?u=https://hillbamboo.gitee.io/2017/05/22/pydoc01/&t=Python tutorial 摘录" target="_blank" rel="noopener"><i class="fab fa-hacker-news fa-lg" aria-hidden="true"></i></a></li> -->
</ul>



    </div>

    <div id="actions-footer">
        <a id="menu" class="icon" href="#" onclick="$('#nav-footer').toggle();return false;"><i class="fas fa-bars fa-lg" aria-hidden="true"></i> 菜单</a>
        <a id="toc" class="icon" href="#" onclick="$('#toc-footer').toggle();return false;"><i class="fas fa-list fa-lg" aria-hidden="true"></i> 目录</a>
        <a id="share" class="icon" href="#" onclick="$('#share-footer').toggle();return false;"><i class="fas fa-share-alt fa-lg" aria-hidden="true"></i> 分享</a>
        <a id="top" style="display:none" class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up fa-lg" aria-hidden="true"></i> 返回顶部</a>
    </div>

  </div>
</div>

        
        <footer id="footer">
  <div class="footer-left">
    Copyright &copy;
    
    
    2017-2022
    拿铁轮
  </div>
  <div class="footer-right">
    <nav>
      <ul>
        <!--
       --><li><a href="/">首页</a></li><!--
     --><!--
       --><li><a href="/about/">关于</a></li><!--
     --><!--
       --><li><a href="/archives/">归档</a></li><!--
     --><!--
       --><li><a href="/categories/">分类</a></li><!--
     --><!--
       --><li><a href="/tags/">标签</a></li><!--
     --><!--
       --><li><a href="/search/">搜索</a></li><!--
     -->
      </ul>
    </nav>
  </div>
</footer>

    </div>
    <!-- styles -->



  <link rel="preload" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css" crossorigin="anonymous" onload="this.onload=null;this.rel='stylesheet'"/>


    <!-- jquery -->
 
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" crossorigin="anonymous"></script> 




<!-- clipboard -->

  
    <script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.7/clipboard.min.js" crossorigin="anonymous"></script> 
  
  <script type="text/javascript">
  $(function() {
    // copy-btn HTML
    var btn = "<span class=\"btn-copy tooltipped tooltipped-sw\" aria-label=\"复制到粘贴板！\">";
    btn += '<i class="far fa-clone"></i>';
    btn += '</span>'; 
    // mount it!
    $(".highlight table").before(btn);
    var clip = new ClipboardJS('.btn-copy', {
      text: function(trigger) {
        return Array.from(trigger.nextElementSibling.querySelectorAll('.code')).reduce((str,it)=>str+it.innerText+'\n','')
      }
    });
    clip.on('success', function(e) {
      e.trigger.setAttribute('aria-label', "复制成功！");
      e.clearSelection();
    })
  })
  </script>


<script src="/js/main.js"></script>

<!-- search -->

<!-- Google Analytics -->

<!-- Baidu Analytics -->

<!-- Cloudflare Analytics -->

<!-- Umami Analytics -->

<!-- Disqus Comments -->

    <script type="text/javascript">
        var disqus_shortname = 'cactus-1';

        (function(){
            var dsq = document.createElement('script');
            dsq.type = 'text/javascript';
            dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
        }());
    </script>

<!-- utterances Comments --><!-- hexo-inject:begin --><!-- Begin: Injected MathJax -->
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({"tex2jax":{"inlineMath":[["$","$"],["\\(","\\)"]],"skipTags":["script","noscript","style","textarea","pre","code"],"processEscapes":true},"TeX":{"equationNumbers":{"autoNumber":"AMS"}}});
</script>

<script type="text/x-mathjax-config">
  MathJax.Hub.Queue(function() {
    var all = MathJax.Hub.getAllJax(), i;
    for(i=0; i < all.length; i += 1) {
      all[i].SourceElement().parentNode.className += ' has-jax';
    }
  });
</script>

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js">
</script>
<!-- End: Injected MathJax -->
<!-- hexo-inject:end -->

</body>
</html>
